package demo06

import scala.collection.mutable

/**
 * @author xiaoZhao
 * @date 2022/7/12
 * @describe 可变的Map
 */
object Test09_MutableMap {
  def main(args: Array[String]): Unit = {
    // 1。创建Map
    val map1: mutable.Map[String, Int] = mutable.Map("a" -> 13, "b" -> 21, "hello" -> 80)
    println(map1)
    println(map1.getClass)

    // 2.添加元素
    map1.put("c",10)
    map1.put("d",21)
    println(map1)

    map1 += (("e",90))
    println(map1)

    // 3.删除元素
    println(map1.remove("e"))
    println(map1.getOrElse("e", 0))

    map1 -= "d"
    println(map1)

    // 4.修改元素
    map1.update("a",21)
    println(map1)

    // 5.合并集合
    val map2: Map[String, Int] = Map("o" -> 9, "y" -> 28, "hello" -> 80)
    // 可变加不可变
    map1 ++= map2
    println(map1)
    println(map2)
    // 不可变加可变
    val map3 = map2 ++ map1
    println(map3)
  }
}
